<template>
  <div>
    <el-row>
            <p><b>首页</b>/下订单/购买商品</p>
    </el-row>

    <el-row>
        <el-table
            :data="productData"
            style="width: 100%"
            >
            <el-table-column
            fixed
            prop="itemName"
            label="商品名"
            >
            </el-table-column>
            <el-table-column
            prop="itemType"
            label="商品类型"
            >
            </el-table-column>
            <el-table-column
            prop="salePrice"
            label="商品价格"
            >
            </el-table-column>
            <el-table-column
            prop="stock"
            label="商品库存"
            >
            </el-table-column>
           
            <el-table-column
            fixed="right"
            label="操作"
            >
            <template slot-scope="scope">
              <el-input-number v-model="scope.row.Num" @change="handleChange" :min="0" :max="10" label="描述文字"></el-input-number>
              <el-button type="primary" @click="purchase(scope.row.id)">购买</el-button>
            </template>
            </el-table-column>
        </el-table>
    </el-row>

    <el-row>
      <el-col>
        <el-pagination
                background
                :page-sizes="[3, 5, 10, 30]"
                :page-size="pageSize"
                :current-page="pageNum"
                layout="prev, pager, next, total, sizes"
                :total="total"
                @next-click="nextPage"
                @prev-click="prevPage"
                @current-change="changePage"
                @size-change="changeSize"
                >
        </el-pagination>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      Num:0,
      productData:[],
      pageSize:10,
      pageNum:1,
      total:0,
      orderId:'',
      orderStatus:'',
    }
  },

  methods: {
    queryAllproduct(){
      let pageData={}
      pageData.pageSize=this.pageSize
      pageData.pageNum=this.pageNum
      this.$axios.post("apig/css-product/getProductList",pageData).then(resp=>{
        // console.log(resp.data)
        if(resp.data.code==200){
          this.productData=resp.data.data.list
          this.total=resp.data.data.total
        }
      })
    },
    handleChange(val){
      this.Num=val
    },

    //查找订单支付状态
    checkOrderStatus(){
      this.$axios.get('apig/css-order/getOrderStatus/'+this.orderId).then(resp=>{
        this.orderStatus=resp.data.data
      })
    },

     

    purchase(id){
      let param={}
      param.buyNum=this.Num
      param.id=id
      this.$axios.post('apig/css-rabbit/addOrder',param).then(resp=>{
              this.queryAllproduct()
              this.orderId=resp.data.data

              //查找订单支付状态
              this.checkOrderStatus()
              setTimeout(() => {
                //查找订单支付状态
                this.checkOrderStatus()
                setTimeout(() => {
                    if(this.orderStatus==0){
                      this.$notify({
                        title: '提示',
                        message: "订单id为:"+this.orderId + resp.data.msg,
                        type: 'warning'
                      });
                    }
                },5000);
            }, 5000); // 延迟 10 秒（15000 毫秒）    
      })
    },

    
   
   

    //下一页方法
    nextPage(val){
                    //回调参数val,val等于当前页码
                    this.pageNum=val
                    this.queryAllproduct()
    },
    //上一页方法
    prevPage(val){
                    //回调参数val,val等于当前页码
                    this.pageNum=val
                    this.queryAllproduct()
    },
    changePage(val){
                    this.pageNum=val
                    this.queryAllproduct()
    },
    changeSize(val){
                    this.pageSize=val
                    this.pageNum=1
                    this.queryAllproduct()
    },

  },
  created(){
    this.queryAllproduct()

  },
}
</script>

<style scoped>
</style>